package com.whfc.fse.service.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.common.enums.NetState;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.fse.dao.*;
import com.whfc.fse.dto.*;
import com.whfc.fse.entity.FseCrane;
import com.whfc.fse.entity.FseCraneData;
import com.whfc.fse.entity.FseLift;
import com.whfc.fse.enums.FseLiftSide;
import com.whfc.fse.service.FseBoardService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author sun_guodong
 * @date 2020-06-15
 */
@DubboService(interfaceClass = FseBoardService.class, version = "1.0.0", timeout = 30 * 1000)
public class FseBoardServiceImpl implements FseBoardService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FseCraneMapper fseCraneMapper;

    @Autowired
    private FseCraneDataMapper fseCraneDataMapper;

    @Autowired
    private FseLiftMapper fseLiftMapper;

    @Autowired
    private FseLiftRecordMapper fseLiftRecordMapper;

    @Autowired
    private FseLiftDataMapper fseLiftDataMapper;

    @Autowired
    private FseWarnMapper fseWarnMapper;

    @Override
    public Integer getFseNum(List<Integer> deptIds) {
        logger.info("查找项目的总特种设备数服务,deptIds:{}", deptIds);
        Integer craneNum = fseCraneMapper.countCraneNum(deptIds, null);
        Integer liftNum = fseLiftMapper.countLiftNum(deptIds, null);
        return craneNum + liftNum;
    }

    @Override
    public Integer getFseOnlineNum(Integer deptId) {
        logger.info("查找项目的总特种设备数服务,deptId:{}", deptId);
        Integer craneNum = fseCraneMapper.countCraneNum(Collections.singletonList(deptId), NetState.ONLINE.getValue());
        Integer liftNum = fseLiftMapper.countLiftNum(Collections.singletonList(deptId), NetState.ONLINE.getValue());
        return craneNum + liftNum;
    }

    @Override
    public FseCraneDataDetailDTO getCraneDataDetail(Integer craneId) throws BizException {
        logger.info("获取塔机运行详情,craneId:{}", craneId);
        //获取塔机详情
        FseCrane fseCrane = fseCraneMapper.selectByPrimaryKey(craneId);
        if (fseCrane == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该塔机不存在");
        }
        FseCraneDataDetailDTO craneDataDetailDTO = new FseCraneDataDetailDTO();
        craneDataDetailDTO.setCode(fseCrane.getCode());
        craneDataDetailDTO.setNetState(fseCrane.getNetState());
        //获取操作手
        String operators = fseCrane.getOperators();
        List<FseOperatorDTO> operatorList = JSON.parseArray(operators, FseOperatorDTO.class);
        StringJoiner stringJoiner = new StringJoiner("、");
        if (operatorList != null) {
            for (FseOperatorDTO fseOperatorDTO : operatorList) {
                stringJoiner.add(fseOperatorDTO.getName());
            }
        }
        craneDataDetailDTO.setOperators(stringJoiner.toString());
        //获取报警次数
        Date startTime = DateUtil.getDateBegin(new Date());
        Date endTime = DateUtil.getDateEnd(startTime);
        fseWarnMapper.countDeviceWarnNum(craneId, 1, startTime, endTime);
        FseCraneData fseCraneData = fseCraneDataMapper.selectByCraneId(craneId);
        if (fseCraneData != null) {
            BeanUtils.copyProperties(fseCraneData, craneDataDetailDTO);
        }
        return craneDataDetailDTO;
    }

    @Override
    public FseLiftDataDetailDTO getLiftDataDetail(Integer liftId) throws BizException {
        logger.info("获取升降机运行详情,liftId:{}", liftId);
        FseLift fseLift = fseLiftMapper.selectByPrimaryKey(liftId);
        if (fseLift == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该升降机不存在");
        }
        FseLiftDataDetailDTO liftDataDetailDTO = new FseLiftDataDetailDTO();
        liftDataDetailDTO.setCode(fseLift.getCode());
        liftDataDetailDTO.setLeftNetState(fseLift.getLeftNetState());
        liftDataDetailDTO.setRightNetState(fseLift.getRightNetState());
        //获取操作手
        String operators = fseLift.getOperators();
        List<FseOperatorDTO> operatorList = JSON.parseArray(operators, FseOperatorDTO.class);
        StringJoiner stringJoiner = new StringJoiner("、");
        if (operatorList != null) {
            for (FseOperatorDTO fseOperatorDTO : operatorList) {
                stringJoiner.add(fseOperatorDTO.getName());
            }
        }
        liftDataDetailDTO.setOperators(stringJoiner.toString());
        //获取报警次数
        Date startTime = DateUtil.getDateBegin(new Date());
        Date endTime = DateUtil.getDateEnd(startTime);
        fseWarnMapper.countDeviceWarnNum(liftId, 2, startTime, endTime);
        //统计升降次数
        List<FseLiftRecordDTO> recordDTOList = fseLiftRecordMapper.selectByLiftIdAndTime(liftId, startTime, endTime);
        liftDataDetailDTO.setLiftNum(recordDTOList.size());
        int leftLiftNum = 0;
        int rightLiftNum = 0;
        for (FseLiftRecordDTO fseLiftRecordDTO : recordDTOList) {
            if (FseLiftSide.LEFT.getValue().equals(fseLiftRecordDTO.getLiftSide())) {
                leftLiftNum += 1;
            } else {
                rightLiftNum += 1;
            }
        }
        liftDataDetailDTO.setLeftLiftNum(leftLiftNum);
        liftDataDetailDTO.setRightLiftNum(rightLiftNum);
        List<FseLiftDataDTO> liftDataList = fseLiftDataMapper.selectByLiftId(liftId);
        liftDataDetailDTO.setList(liftDataList);
        return liftDataDetailDTO;
    }
}
